//
//  MainTabBar.m
//  NongWanCloud
//
//  Created by YueWen on 2018/2/11.
//  Copyright © 2018年 YueWen. All rights reserved.
//

#import "QDMainTabBar.h"

@interface QDMainTabBar ()

@property (nonatomic, strong, readwrite) RITLItemButton *largeButton;
/// 背景ImageView
@property (nonatomic, strong) UIImageView * backgroundImageView;
/// 背景图层
@property (nonatomic, strong) CALayer *backgroundLayer;

@end

@implementation QDMainTabBar


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.largeButton = [RITLItemButton new];
        self.largeButton.imageViewEdgeInsets = UIEdgeInsetsMake(0, 0, 5, 0);
        self.largeButton.titleLabelEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        self.largeButton.titleLabel.hidden = true;
        self.largeButton.titleLabel.backgroundColor = UIColor.clearColor;
        self.largeButton.titleLabel.font = RITLUtilityFont(RITLFontPingFangSC_Medium, 10);
        self.largeButton.titleLabel.textColor = RITLColorFromRGB(0x929292);
        self.largeButton.imageSize = CGSizeMake(60, 60);
        self.largeButton.imageView.backgroundColor = UIColor.clearColor;
        self.largeButton.backgroundColor = UIColor.clearColor;
        [self addSubview:self.largeButton];
        
//        if (RITL_HAVE_PHONE_HEADER) {
//            self.backgroundLayer = CALayer.layer;
//            self.backgroundLayer.anchorPoint = CGPointZero;
//            self.backgroundLayer.position = CGPointZero;
//            self.backgroundLayer.ritl_size = CGSizeMake(UIScreen.mainScreen.ritl_width, RITL_DefaultTabBarHeight);
//            self.backgroundLayer.backgroundColor = UIColor.clearColor.CGColor;
//            [self.layer insertSublayer:self.backgroundLayer atIndex:0];
//        }
        

        
        self.backgroundImageView = UIImageView.new;
        self.backgroundImageView.image = [@"tab_background".ritl_image resizableImageWithCapInsets:UIEdgeInsetsMake(30, 0, 10, 0) resizingMode:UIImageResizingModeTile];
        self.backgroundImageView.backgroundColor = UIColor.clearColor;
        [self addSubview:self.backgroundImageView];
        [self insertSubview:self.backgroundImageView atIndex:0];
        
//        self.opaque = true;
        self.translucent = false;
        self.barTintColor = [UIColor.whiteColor colorWithAlphaComponent:0.01];
        self.backgroundColor = UIColor.clearColor;
        self.shadowImage = [UIColor.whiteColor colorWithAlphaComponent:0.01].ritl_image;
        self.backgroundImage = UIColor.clearColor.ritl_image;
     
    }
    return self;
}


- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    self.largeButton.imageView.image = (isSelected ? @"tab_code_selected" :  @"tab_code").ritl_image;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.backgroundImageView.frame = self.bounds;
    self.backgroundImageView.ritl_height = self.ritl_height + 8;
    self.backgroundImageView.ritl_originY = -8;
    
    self.largeButton.ritl_height = 74;
    self.largeButton.ritl_width = 60;
    self.largeButton.ritl_originY = -20;
    self.largeButton.ritl_centerX = self.ritl_width / 2.0;
    [self bringSubviewToFront:self.largeButton];
    
    //进行位置调整
//
//    if (self.subviews.count <= 1) return;
//
//    //获得第一个的左边距
//    CGFloat leftSpace = self.subviews.firstObject.ritl_originX;
//
//    NSInteger i = 0;
    
}


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    for (UIView *subView in self.subviews) {
        
        CGPoint tp = [subView convertPoint:point fromView:self];
        
        if (CGRectContainsPoint(subView.bounds, tp)) {
            
            return true;
        }
    }
    return [super pointInside:point withEvent:event];
}




- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    
    if (view == nil && self.hidden == false) {
        
        for (UIView *subView in self.subviews) {
            
            CGPoint tp = [subView convertPoint:point fromView:self];
            
            if (CGRectContainsPoint(subView.bounds, tp)) {
                
                view = subView;
            }
        }
    }
    return view;
}

@end
